/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.entitysubsystem;

import assisisw.smartcal.entitysubsystem.exceptions.user.UserAlreadyRegisteredException;
import assisisw.smartcal.entitysubsystem.exceptions.user.UserNotRegisteredException;
import assisisw.smartcal.persistencesubsystem.IDAOProvider;
import assisisw.smartcal.persistencesubsystem.IUserDAO;
import assisisw.smartcal.securitysubsystem.PasswordManager;
import java.util.Objects;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

/**
 *
 * @author Francesco
 */
@Stateless ()
@TransactionManagement ( value = TransactionManagementType.CONTAINER )
public class UserController implements IUserController 
{
    
    // Instance Fields
    
    /***/
    @EJB ()
    private IDAOProvider daoProvider ;
    
    // Instance Methods
    
    /***/
    public UserController () {}
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public boolean isRegistered ( String mail )
    {
        User u ;
        boolean res ;
        try 
        {
            u = getUser ( mail ) ;
            res = true ;
        } 
        catch ( UserNotRegisteredException ex ) 
        {
            res = false ;
        }
        return res ;
    }
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void registerUser ( String eMail, String password, String firstName, String lastName, String token) 
            throws UserAlreadyRegisteredException 
    {
        PasswordManager passwordManager ;
        IUserDAO userDAO ;
        User newUser ;
        userDAO = daoProvider.getUserDAO () ;
        newUser = userDAO.findByMail ( eMail ) ;
        if ( newUser == null )
        {
            passwordManager = new PasswordManager () ;
            newUser = new User ( eMail.toLowerCase() , firstName , lastName , passwordManager.hash(password) ) ;
            newUser.setToken ( token ) ; 
            userDAO.insert ( newUser ) ; 
        }
        else
            throw new UserAlreadyRegisteredException ( newUser ) ;
    }
     
    /**
     * @param u
     * @throws IllegalArgumentException
     */
    @Override
    public void activate ( User u )
    {
        User toUpdate ;
        if ( Objects.nonNull ( u ) )
        {
            toUpdate = daoProvider.getUserDAO().findByMail ( u.getMail() );
            toUpdate.activate();
            daoProvider.getUserDAO ().update ( toUpdate ) ; 
            u.activate () ;
        }
        else
            throw new IllegalArgumentException () ;
    }
    
    /**
     * @param u
     * @throws IllegalArgumentException
     */
    @Override
    public void deactivate ( User u ) 
    {
        if ( u != null )
        {
            u.deactivate () ;
            daoProvider.getUserDAO ().update ( u ) ;
        }
        else
            throw new IllegalArgumentException () ;
    }
    
    /**
     * @param eMail
     * @return
     * @throws IllegalArgumentException
     * @throws UserNotRegisteredException
     */
    @Override
    public User getUser ( String eMail ) throws UserNotRegisteredException 
    {
        User res ;
        if ( eMail != null && ! eMail.isEmpty () )
        {
            res = daoProvider.getUserDAO ().findByMail ( eMail ) ;
            if ( res == null )
                throw new UserNotRegisteredException ( eMail ) ;
            else
                res.getOwnedCalendars().size();
        }
        else
            throw new IllegalArgumentException () ;
        return res ;
    }
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void update ( User u ) 
    {
        User persisted ;
        persisted = daoProvider.getUserDAO().findByMail(u.getMail ());
        User.copySimpleData ( u , persisted ) ;
        daoProvider.getUserDAO().update(u); 
    }
    
}

